package cqut.wys;

import org.rocksdb.Options;
import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;

/**
 * --------神兽出没--------/
 *   ┌─┐     ┌─┐
 * ┌─┘─┴─────┘─┴─┐
 * │      ─      │
 * │  ┬─┘   └─┬  │
 * │             │
 * │      ┴      │  Code is far away from bug with the animal protecting
 * └───┐      ┌──┘  神兽保佑,代码无bug
 *     │      │
 *     │      └──────┐
 *     │             ├┐
 *     │             ┌┘
 *     └┐ ┐ ┌───┬─┐ ┌┘
 *      │ ┤ ┤   │ ┤ ┤
 *      └─┴─┘   └─┴─┘
 * -----------------------/
 *
 *  RocksDbOperating 操作
 *
 * @author wys
 * @date 2019/11/7
 */
public class RocksDbOperat {

    private static final String DB_PATH = System.getProperty("user.dir") + "\\data";

    private static RocksDB rocksDB;

    static {
        RocksDB.loadLibrary();
    }

    public RocksDbOperat() {
        Options options = new Options();
        options.setCreateIfMissing(true);
        try {
            rocksDB = RocksDB.open(options, DB_PATH);
        } catch (RocksDBException e) {
            e.printStackTrace();
        }
    }

    public String put(String key,String value) throws RocksDBException {
        rocksDB.put(key.getBytes(), value.getBytes());
        return "ok";
    }

    public String get(String key) throws Exception {
        byte[] values = rocksDB.get(key.getBytes());
        if(values != null){
            return new String(values,"utf-8");
        }else {
            return null;
        }
    }
}
